package IC.Semantics.symbols;

public class BlockSymbolTable extends SymbolTable
{
	protected int currentChildId;
	
	public BlockSymbolTable(SymbolTable parent) 
	{
		super(parent,  "statement block in " + parent.name);
	}
	
	public void addTable(SymbolTable st){
		super.addTable(String.valueOf(++currentChildId), st);
	}
	
	public ClassSymbolTable getClassScope(){
		SymbolTable scope = parent;
		while (!(scope instanceof ClassSymbolTable))
			scope = scope.getParent();
		return (ClassSymbolTable) scope;
	}

	@Override
	public String dump() 
	{
		StringBuilder str = new StringBuilder("Statement Block Symbol Table ( located in " + parent.name + " )");
		
		for (SemanticSymbol s : entries.values())
			 str.append("\n\tLocal variable: "+s.getType().toString() +" "+s.getName());
		
		if (!children.isEmpty())
		{
			str.append("\nChildren tables: ");
			for (SymbolTable bst : children.values())
				str.append(bst.name + ", ");
			
			str.delete(str.length()-2, str.length());
		}
		
		str.append("\n\n");
		for (SymbolTable bst : children.values())
			str.append(bst.dump());
		
		return str.toString();
	}

}
